package com.example.app;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class VelocityActivity extends ActionBarActivity {


    public void onVeloClick(View view) {
        double city, height, answer;

        EditText velo = (EditText) findViewById(R.id.text_velocity);

        String stringVel = velo.getText().toString();

        EditText distance = (EditText) findViewById(R.id.text_distance);

        String stringDis = distance.getText().toString();

        EditText time = (EditText) findViewById(R.id.text_time);

        String stringTime = time.getText().toString();

        Boolean distanceFilled = !(stringTime == null || stringTime.length() == 0);

        Boolean timeFilled = !(stringDis == null || stringDis.length() == 0);

        Boolean velFilled = !(stringVel == null || stringVel.length() == 0);

        Boolean solved = false;

        if (stringTime == null || stringTime.length() == 0) {
            city = 0;
        } else {
            city = Double.parseDouble(stringTime);
        }

        if (stringDis == null || stringDis.length() == 0) {
            height = 0;
        } else {
            height = Double.parseDouble(stringDis);
        }
        if(solved&&velFilled){
            velFilled = false;
            solved = false;
        }
        if (!solved && distanceFilled && timeFilled) {

            String calString = (height/city) + "";

            EditText calculation = (EditText) findViewById(R.id.text_velocity);
            calculation.setText(calString);
            solved = true;
        }


    }
    public void onDisClick(View view){
        double city, height, answer;

        EditText velo = (EditText) findViewById(R.id.text_velocity);

        String stringVel = velo.getText().toString();

        EditText distance = (EditText) findViewById(R.id.text_distance);

        String stringDis = distance.getText().toString();

        EditText time = (EditText) findViewById(R.id.text_time);

        String stringTime = time.getText().toString();

        Boolean distanceFilled = !(stringTime == null || stringTime.length() == 0);

        Boolean timeFilled = !(stringDis == null || stringDis.length() == 0);

        Boolean velFilled = !(stringVel == null || stringVel.length() == 0);

        Boolean solved = false;

        if (stringTime == null || stringTime.length() == 0) {
                city = 0;
            } else {
                city = Double.parseDouble(stringTime);
            }

        if (stringVel == null || stringVel.length() == 0) {
                answer = 0;
            } else {
                answer = Double.parseDouble(stringVel);
            }
        if(solved&&distanceFilled){
            distanceFilled = false;
            solved = false;
        }
        if (timeFilled && velFilled && !solved) {

                String calString = (city * answer) + "";

            EditText calculation = (EditText) findViewById(R.id.text_distance);
            calculation.setText(calString);
            solved = true;
        }
    }

    public void onTimeClicked (View view){
        double city, height, answer;

        EditText velo = (EditText) findViewById(R.id.text_velocity);

        String stringVel = velo.getText().toString();

        EditText distance = (EditText) findViewById(R.id.text_distance);

        String stringDis = distance.getText().toString();

        EditText time = (EditText) findViewById(R.id.text_time);

        String stringTime = time.getText().toString();

        Boolean distanceFilled = !(stringTime == null || stringTime.length() == 0);

        Boolean timeFilled = !(stringDis == null || stringDis.length() == 0);

        Boolean velFilled = !(stringVel == null || stringVel.length() == 0);

        Boolean solved = false;



        if (stringDis == null || stringDis.length() == 0) {
            height = 0;
        } else {
            height = Double.parseDouble(stringDis);
        }

        if (stringVel == null || stringVel.length() == 0) {
            answer = 0;
        } else {
            answer = Double.parseDouble(stringVel);
        }
        if(solved&&timeFilled){
            timeFilled = false;
            solved = false;
        }
        if (distanceFilled && velFilled && !solved) {

            String calString = (answer * height) + "";

            EditText calculation = (EditText) findViewById(R.id.text_time);
            calculation.setText(calString);
            solved = true;

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_velocity);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.velocity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public class PlaceholderFragment extends Fragment {
        /*
                boolean distanceChanged = false;
                boolean timeChanged = false;
                boolean velChanged = false;
                boolean changing = false;
                double distance = 0;
                double time = 0;
                double vel = 0;
*/
                public PlaceholderFragment() {
                }
/*
                double city, height, answer;
                boolean solved = false;

                public void calculate() {
                    changing = true;
                    if (distanceChanged && timeChanged) {
                        Toast.makeText(getActivity(), "hello", Toast.LENGTH_SHORT).show();

                        vel = (distance / time);
                        EditText calculation = (EditText) findViewById(R.id.text_velocity);
                        calculation.setText(vel + "");
                        distanceChanged = false;
                        timeChanged = false;
                        velChanged = true;
                    }
                    if (distanceChanged && velChanged) {
                        Toast.makeText(getActivity(), "hello", Toast.LENGTH_SHORT).show();

                        String calcOne = (vel * distance) + "";
                        EditText calculation = (EditText) findViewById(R.id.text_time);
                        calculation.setText(calcOne);
                        distanceChanged = false;
                        velChanged = false;
                    }
                    if (velChanged && timeChanged) {
                        Toast.makeText(getActivity(), "hello", Toast.LENGTH_SHORT).show();

                        String calcOne = (vel * time) + "";
                        EditText calculation = (EditText) findViewById(R.id.text_distance);
                        calculation.setText(calcOne);
                        velChanged = false;
                        timeChanged = false;
                    }
                    changing = false;
                }
                */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_velocity, container, false);
                return rootView;

        }
}}


